package com.jz.community.moduleshoppingguide.nearshop.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.jz.community.commview.view.RelativePopupWindow;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.nearshop.bean.CategoriesBean;
import com.jz.community.moduleshoppingguide.nearshop.ui.adapter.NearShopTypeAdapter;

import java.util.List;

public class ShopCategorysPop extends RelativePopupWindow implements NearShopTypeAdapter.TypeChoose, PopupWindow.OnDismissListener {


    private Context context;
    View view;
    NearShopTypeAdapter nearShopTypeAdapter;
    private List<CategoriesBean.EmbeddedBeanXX.ContentBean> typelist;
    PopTypeChoose popTypeChoose;
    ImageView nearShopSelAllIv;

    public ShopCategorysPop(Context context, List<CategoriesBean.EmbeddedBeanXX.ContentBean> typelist, ImageView nearShopSelAllIv) {
        super(context);
        this.context = context;
        this.typelist = typelist;
        this.nearShopSelAllIv = nearShopSelAllIv;
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
        view = View.inflate(context, R.layout.pop_near_shop, null);
        setContentView(view);
        initView(view);
    }

    private void initView(View view) {
        RecyclerView popRv = view.findViewById(R.id.pop_near_shop_rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        popRv.setHasFixedSize(true);
        popRv.setFocusable(false);
        popRv.setLayoutManager(gridLayoutManager);
        nearShopTypeAdapter = new NearShopTypeAdapter(R.layout.module_shoppingguide_item_pop_near, typelist);
        popRv.setAdapter(nearShopTypeAdapter);
        nearShopTypeAdapter.setTypeChoose(this);
        View pop_shade = view.findViewById(R.id.pop_near_shop_shade);
        pop_shade.setOnClickListener(v -> dismiss());
        setOnDismissListener(this);
    }


    @Override
    public void currentChoose(CategoriesBean.EmbeddedBeanXX.ContentBean entity) {
        popTypeChoose.popChoose(entity);
        dismiss();
    }

    @Override
    public void onDismiss() {
        nearShopSelAllIv.setImageResource(R.mipmap.near_shop_all_down);
    }


    public interface PopTypeChoose {
        void popChoose(CategoriesBean.EmbeddedBeanXX.ContentBean entity);
    }

    public void setPopTypeChoose(PopTypeChoose popTypeChoose) {
        this.popTypeChoose = popTypeChoose;
    }

    @Override
    public void showOnAnchor(@NonNull View anchor, int vertPos, int horizPos, int x, int y) {
        super.showOnAnchor(anchor, vertPos, horizPos, x, y);
        nearShopSelAllIv.setImageResource(R.mipmap.near_shop_all_up);
    }


    public NearShopTypeAdapter getPopAdapter() {
        return nearShopTypeAdapter;
    }
}
